package com.bite.config.demo.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ICaptcha;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.lang.Console;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

//验证码案例
@RestController
public class CaptchaController {

    @RequestMapping("/getCaptcha")
    public void getCaptcha(HttpServletResponse response){
        //定义图形验证码的长和宽
        //LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
        //图形验证码写出，可以写出到文件，也可以写出到流
        //lineCaptcha.write("d:/line.png");
        //输出code
        //Console.log(lineCaptcha.getCode());
        //验证图形验证码的有效性，返回boolean值
        //lineCaptcha.verify("1234");

        ICaptcha captcha = CaptchaUtil.createLineCaptcha(200, 100);;
        try {
            captcha.write(response.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
